# 公用的自定义工具类
from flask import session, current_app, g

import functools


from info.models import User


def do_index_class(index):
    """返回指定索引对应的类名"""

    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    return ""


def query_user_data(func):
    # 使用 @functools.wraps,去装饰函数, 可以保持当前装饰器去装饰的函数名字不变, 如果两个函数名字相同的话,flask会报错
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id", None)
        user = None
        if user_id:
            # 尝试查询用户模型
            try:
                from info.models import User
                user = User.query.get(user_id)
            except Exception as f:
                current_app.logger.error(f)
        g.user = user
        return func(*args, **kwargs)  # 拆包

    return wrapper


# def query_user_data():
#     user_id = session.get("user_id", None)
#     user = None
#     if user_id:
#         # 尝试查询用户模型
#         try:
#             from info.models import User
#             user = User.query.get(user_id)
#         except Exception as f:
#             current_app.logger.error(f)
#     return
